package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.SchoolQuarter;
import com.xmy.cultivate.entity.response.RepairLessonRs;
import com.xmy.cultivate.service.IRecordCourseService;
import com.xmy.cultivate.service.ISchoolQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 补课人数
 * </p>
 *
 * @author min
 * @since 2022-11-01
 */
@RestController
@RequestMapping("/api/repairLesson")
public class RepairLessonController extends BaseController {

    @Autowired
    IRecordCourseService iRecordCourseService;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    /**
     * 获取补课人数
     */
    @GetMapping("/getRepairLesson")
    public Result getRepairLesson(@RequestParam(value = "yearPart", required = false, defaultValue = "") String yearPart,
                                  @RequestParam(value = "quarterNum", required = false, defaultValue = "") String quarterNum,
                                  @RequestParam(value = "schoolId", required = false, defaultValue = "") String schoolId,
                                  @RequestParam(value = "subjectsId", required = false, defaultValue = "") String subjectsId,
                                  @RequestParam(value = "type", required = false, defaultValue = "") String type,
                                  @RequestParam(value = "startDate", required = false, defaultValue = "") String startDate,
                                  @RequestParam(value = "endDate", required = false, defaultValue = "") String endDate,
                                  @RequestParam(value = "yearClassId", required = false, defaultValue = "") String yearClassId,
                                  @RequestParam(value = "classify", required = false, defaultValue = "") String classify,
                                  @RequestParam(value = "trackType", required = false, defaultValue = "") String trackType,
                                  @RequestParam(value = "teamId", required = false, defaultValue = "") String teamId,
                                  @RequestParam(value = "lessonType", required = false, defaultValue = "1") String lessonType) {

        List<RepairLessonRs> repairLessonRsList = new ArrayList<>();

        if (type.equals("5")) {
            repairLessonRsList = iRecordCourseService.getRepairLessonTeamList(yearPart, quarterNum, type, subjectsId, startDate, endDate, Integer.parseInt(lessonType), yearClassId, classify, trackType, teamId);
        } else {
            repairLessonRsList = iRecordCourseService.getRepairLesson(this.baseSchoolIdStr, schoolId, yearPart, quarterNum, type, subjectsId, startDate, endDate, Integer.parseInt(lessonType), yearClassId);

            if (type.equals("6") && StringUtils.isBlank(schoolId)) {
                List<RepairLessonRs> collectList = new ArrayList<>();

                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("year_part", yearPart);
                queryWrapper.eq("quarter_num", quarterNum);
                queryWrapper.eq("pid", 0);
                List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

                for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                    RepairLessonRs repairLessonRs = new RepairLessonRs();
                    repairLessonRs.setSchoolId(schoolQuarter.getSchoolId());
                    repairLessonRs.setSchoolName(schoolQuarter.getSchoolName());
                    repairLessonRs.setSchoolPid(schoolQuarter.getPid());
                    repairLessonRs.setAllRepairCount(0);
                    repairLessonRs.setRepairCount(0);

                    collectList.add(repairLessonRs);
                }

                for (RepairLessonRs repairLessonRs : collectList) {
                    List<RepairLessonRs> childRates = new ArrayList<>();
                    for (RepairLessonRs lessonRs : repairLessonRsList) {
                        if (repairLessonRs.getSchoolId().equals(lessonRs.getSchoolId()) || repairLessonRs.getSchoolId().equals(lessonRs.getSchoolPid())) {
                            childRates.add(lessonRs);
                            if (lessonRs.getAllRepairCount() != null) {
                                repairLessonRs.setAllRepairCount(repairLessonRs.getAllRepairCount() + lessonRs.getAllRepairCount());
                            }
                            if (lessonRs.getRepairCount() != null) {
                                repairLessonRs.setRepairCount(repairLessonRs.getRepairCount() + lessonRs.getRepairCount());
                            }
                        }
                    }
                    repairLessonRs.setChildRateList(childRates);
                }
                repairLessonRsList = collectList;
            }

        }

        return new Result(Code.OK, repairLessonRsList, Code.GET_OK_MSG);
    }

    /**
     * 获取补课人数详情
     */
    @GetMapping("/getRepairLessonDetail")
    public Result getRepairLessonDetail(@RequestParam(value = "yearPart", required = true, defaultValue = "") String yearPart,
                                        @RequestParam(value = "quarterNum", required = true, defaultValue = "") String quarterNum,
                                        @RequestParam(value = "schoolId", required = true, defaultValue = "") String schoolId,
                                        @RequestParam(value = "gradeId", required = false, defaultValue = "") String gradeId,
                                        @RequestParam(value = "teacherId", required = true, defaultValue = "") Long teacherId,
                                        @RequestParam(value = "subjectsId", required = false, defaultValue = "") String subjectsId,
                                        @RequestParam(value = "type", required = false, defaultValue = "") String type,
                                        @RequestParam(value = "startDate", required = false, defaultValue = "") String startDate,
                                        @RequestParam(value = "endDate", required = false, defaultValue = "") String endDate,
                                        @RequestParam(value = "studentName", required = false, defaultValue = "") String studentName,
                                        @RequestParam(value = "yearClassId", required = false, defaultValue = "") String yearClassId,
                                        @RequestParam(value = "classify", required = false, defaultValue = "") String classify,
                                        @RequestParam(value = "trackType", required = false, defaultValue = "") String trackType,
                                        @RequestParam(value = "teamId", required = false, defaultValue = "") String teamId,
                                        @RequestParam(value = "lessonType", required = false, defaultValue = "1") String lessonType) {

        List<RepairLessonRs> repairLessonRsList = new ArrayList<>();

        if (type.equals("5")) {
            repairLessonRsList = iRecordCourseService.getRepairLessonTeamDetail(yearPart, quarterNum, type, subjectsId, startDate, endDate, Integer.parseInt(lessonType), teacherId, gradeId, studentName, yearClassId, classify, trackType, teamId);
        } else {
            repairLessonRsList = iRecordCourseService.getRepairLessonDetail(schoolId, yearPart, quarterNum, type, subjectsId, startDate, endDate, Integer.parseInt(lessonType), teacherId, gradeId, studentName, yearClassId);
        }

        return new Result(Code.OK, repairLessonRsList, Code.GET_OK_MSG);
    }

}
